package org.nerve.android.ui.dialog;

import java.util.List;

import org.nerve.android.ui.dialog.DialogHelper.ConfirmListener;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;



/**
 * @项目名称 :nerveAndroid
 * @文件名称 :ConfirmDialog.java
 * @所在包 :org.nerve.ui.dialog
 * @功能描述 :
 *	选择对话框
 *
 *	加载指定layout，并搜索指定tag 的元素，然后对搜索到的View加一个点击事件
 *	点击后，返回的是这个View所在ViewGroup 出现的序号
 *
 * @创建者 :集成显卡	1053214511@qq.com
 * @公司：广西可诺科技有限公司(http://www.chonour.com/)
 * @创建日期 :2013-6-6
 * @修改记录 :
 */
public class SpinnerDialog {
	
	protected Context context;
	protected Dialog dialog;
	
	protected OnClickListener clickListener;
	
	protected ConfirmListener listener;
	
	private String values[];
	private String title;
	private int code;
	
	
	private SpinnerDialog(Context context){
		this.context = context;
		
		clickListener = new OnClickListener() {
			@Override
			public void onClick(View v) {
				int index = v.getId();
				
				if(listener != null)
					listener.onConfirmClick(index, code);
				
				dialog.dismiss();
			}
		};
	}
	
	public SpinnerDialog(Context context, String[] values){
		this(context);
		
		this.values = values;
	}
	
	public SpinnerDialog(Context context, List<String> list){
		this(context);
		
		this.values = new String[list.size()];
		for(int i=0;i<values.length;i++)
			values[i] = list.get(i);
	}
	
	public void setConfirmListener(ConfirmListener listener){
		this.listener = listener;
	}
	
	/**
	 * 创建对话框
	 */
	protected void createDialog(){
		LinearLayout ll = buildLayout();
		
		dialog = new Dialog(context);
		dialog.show();
		Window win = dialog.getWindow();
		//将dialog的背景透明化
		win.setBackgroundDrawable(new ColorDrawable(0));
		
		win.setLayout(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
		win.setContentView(ll);
	}
	
	private LinearLayout buildLayout(){
		LinearLayout ll = new LinearLayout(context);
		ll.setOrientation(LinearLayout.VERTICAL);
		ll.setBackgroundColor(Color.parseColor("#dadada"));
		
		LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, 70);
		Drawable arrowDrawable = null;
		arrowDrawable = context.getResources().getDrawable(android.R.drawable.ic_menu_help);
		arrowDrawable.setBounds(0, 0, arrowDrawable.getMinimumWidth(), arrowDrawable.getMinimumHeight());
		
		TextView titleTV = buildItem(title);
		titleTV.setGravity(Gravity.CENTER_VERTICAL);
		titleTV.setCompoundDrawables(arrowDrawable, null, null, null);
		titleTV.setTextSize(15);
		ll.addView(titleTV, lp);
		
		lp.setMargins(0, 1, 0, 0);
		for(int i=0;i<values.length;i++){
			TextView tv = buildItem("      " + values[i]);
			tv.setGravity(Gravity.CENTER_VERTICAL);
			tv.setOnClickListener(clickListener);
			tv.setId(i);
			tv.setTextSize(15);
			
			ll.addView(tv, lp);
		}
		
		return ll;
	}
	
	private TextView buildItem(String msg){
		TextView tv = new TextView(context);
		tv.setText(msg);
		tv.setTextColor(context.getResources().getColor(android.R.color.black));
		tv.setBackgroundColor(Color.parseColor("#fefefe"));
		
		return tv;
	}
	
	public void show(){
		if(dialog == null)
			createDialog();
		else{
			dialog.show();
		}
	}
	
	/**
	 * @method name: setTitle
	 * @description: 这个需要在show()前调用
	 * @return type: void
	 *
	 * @param title
	 */
	public void setTitle(String title){
		this.title = title;
	}
	
	/**
	 * @method name: setCode
	 * @description: 设置标识
	 * @return type: void
	 *
	 * @param code
	 */
	public void setCode(int code){
		this.code = code;
	}
}